<template>
  <el-dialog v-loading="loading" :append-to-body="true" :close-on-click-modal="false" :visible.sync="visable" title="修改上级" width="500px" top="10vh" @close="closeDlg">
    <p style="color:orange;margin-top:-12px;">调整组织结构可能会影响用户的数据权限；<br>调整后请修改相关资产的使用公司/部门对应关系；</p>

    <el-form ref="xform" :model="form" size="small" label-width="80px">
      <el-form-item label="编号" prop="code">
        <el-input v-model="form.code" maxlength="20" disabled />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" autocomplete="false" maxlength="40" disabled />
      </el-form-item>
      <el-col :span="12">
        <el-form-item label="上级" prop="parentId">
          <select-tree v-model="form.parentId" :options="deptTreeData" :default-expanded-keys="[1]" width="300" placeholder="空，表示是一级公司" clearable />
        </el-form-item>
      </el-col>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import SelectTree from '@/components/SelectTree'
import request from '@/utils/request'
import { resetCache } from '@/api/basedata'
export default {
  name: 'DeptParentForm',
  components: {
    SelectTree
  },
  data() {
    return {
      loading: false,
      visable: false,
      deptTreeData: [],
      oldParentId: null,
      form: {
        id: null,
        name: '',
        code: '',
        parentId: ''
      }
    }
  },
  methods: {
    initDeptOptions(deptOptions) {
      this.deptTreeData = deptOptions
    },
    resetFields() {
      this.oldParentId = null
      this.form = {
        id: null,
        name: '',
        code: '',
        parentId: ''
      }
    },
    cancel() {
      this.closeDlg()
    },
    doSubmit() {
      this.$refs['xform'].validate((valid) => {
        if (valid) {
          if (this.oldParentId === this.form.parentId) {
            this.closeDlg()
            return
          }
          if (this.form.id === this.form.parentId) {
            this.$message.error('上级不能是自已！')
            return
          }
          this.loading = true
          this.doEdit()
          // --
          resetCache('department')
        }
      })
    },

    edit(item) {
      this.resetFields()
      this.form.id = item.id
      this.form.name = item.label
      this.form.code = item.code
      this.form.parentId = item.parentId
      this.oldParentId = item.parentId
      this.visable = true
    },

    doEdit() {
      const param = {}
      param.id = this.form.id
      param.parentId = this.form.parentId
      request({ url: '/sys/department/changeParent', method: 'post', params: param }).then(res => {
        this.closeDlg()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.getList()
      }).catch(err => {
        this.loading = false
        this.$message.error('修改错误' + err.message)
      })
    },
    closeDlg() {
      this.visable = false
      this.resetFields()
    }
  }
}
</script>
